package com.jinwei.qunguanjia.system.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.jinwei.qunguanjia.system.dao.SysDictTypeDAO;
import com.jinwei.qunguanjia.system.dto.SysDictDataDTO;
import com.jinwei.qunguanjia.system.dto.SysDictTypeDTO;
import com.jinwei.qunguanjia.system.po.SysDictTypePO;
import com.jinwei.qunguanjia.common.constant.UserConstants;
import com.jinwei.qunguanjia.common.core.domain.entity.SysDictData;
import com.jinwei.qunguanjia.common.core.domain.model.Condition;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.utils.*;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 字典 服务层实现
 *
 * @author ruoyi
 */
@Service("sysDictTypeService")
public class SysDictTypeServiceImpl implements SysDictTypeService {

    @Resource
    private SysDictTypeDAO sysDictTypeDAO;

    @Resource
    private SysDictDataService sysDictDataService;

    @Override
    public void save(SysDictTypeDTO sysDictTypeDTO) {
        SysDictTypePO sysDictTypePO = new SysDictTypePO();
        BeanUtils.copyProperties(sysDictTypeDTO, sysDictTypePO);
        sysDictTypePO.setDictTypeId(IdUtils.getWorkerId());
        sysDictTypeDAO.insert(sysDictTypePO);
    }

    @Override
    public void remove(List<String> ids) {
        LambdaQueryWrapper<SysDictTypePO> lambdaWrapper = new LambdaQueryWrapper<>();
        lambdaWrapper.in(SysDictTypePO::getDictTypeId, ids);
        sysDictTypeDAO.delete(lambdaWrapper);
    }

    @Override
    public void update(SysDictTypeDTO sysDictTypeDTO) {
        SysDictTypePO sysDictTypePO = new SysDictTypePO();
        BeanUtils.copyProperties(sysDictTypeDTO, sysDictTypePO);
        sysDictTypeDAO.updateById(sysDictTypePO);
    }

    @Override
    public SysDictTypeDTO get(String id) {
        SysDictTypePO sysDictTypePO = sysDictTypeDAO.selectById(id);
        SysDictTypeDTO sysDictTypeDTO = new SysDictTypeDTO();
        BeanUtils.copyProperties(sysDictTypePO, sysDictTypeDTO);
        return sysDictTypeDTO;
    }

    @Override
    public SysDictTypeDTO get(SysDictTypeDTO sysDictTypeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysDictTypeDTO);
        // 拼接查询条件
        QueryWrapper<SysDictTypePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        SysDictTypePO sysDictTypePO = sysDictTypeDAO.selectOne(queryWrapper);
        if (null == sysDictTypePO){
            return null;
        }
        BeanUtils.copyProperties(sysDictTypePO, sysDictTypeDTO);
        return sysDictTypeDTO;
    }

    @Override
    public Integer count(SysDictTypeDTO sysDictTypeDTO) {
        return null;
    }

    @Override
    public List<SysDictTypeDTO> list(SysDictTypeDTO sysDictTypeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysDictTypeDTO);
        // 拼接查询条件
        QueryWrapper<SysDictTypePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        return JsonUtils.copyList(sysDictTypeDAO.selectList(queryWrapper), SysDictTypeDTO.class);
    }

    @Override
    public PageDataInfo<SysDictTypeDTO> listPage(SysDictTypeDTO sysDictTypeDTO) throws IllegalAccessException {
        // 添加查询条件
        List<Condition> conditionList = ConditionUtils.add(sysDictTypeDTO);
        // 拼接查询条件
        QueryWrapper<SysDictTypePO> queryWrapper = SearchUtils.parseWhereSql(conditionList);
        List<SysDictTypePO> list = sysDictTypeDAO.selectList(queryWrapper);
        PageInfo<SysDictTypePO> pageInfo = new PageInfo<>(list);
        PageDataInfo<SysDictTypeDTO> pageDataInfo = new PageDataInfo<>();
        pageDataInfo.setData(JsonUtils.copyList(list, SysDictTypeDTO.class));
        pageDataInfo.setTotal(pageInfo.getTotal());
        return pageDataInfo;
    }

    @Override
    public void loadingDictCache() throws IllegalAccessException {
        SysDictDataDTO dictData = new SysDictDataDTO();
        dictData.setDictDataStatus("0");
        Map<String, List<SysDictData>> dictDataMap = JsonUtils.copyList(sysDictDataService.list(dictData), SysDictData.class).stream().collect(Collectors.groupingBy(SysDictData::getDictType));
        for (Map.Entry<String, List<SysDictData>> entry : dictDataMap.entrySet()) {
            DictUtils.setDictCache(entry.getKey(), entry.getValue().stream().sorted(Comparator.comparing(SysDictData::getDictSort)).collect(Collectors.toList()));
        }
    }

    @Override
    public void clearDictCache() {
        DictUtils.clearDictCache();
    }

    @Override
    public void resetDictCache() throws IllegalAccessException {
        clearDictCache();
        loadingDictCache();
    }

    @Override
    public String checkDictTypeUnique(SysDictTypeDTO sysDictTypeDTO) throws IllegalAccessException {
        String dictId = StringUtils.isNull(sysDictTypeDTO.getDictTypeId()) ? "-1" : sysDictTypeDTO.getDictTypeId();
        SysDictTypeDTO info = get(SysDictTypeDTO.builder().dictType(sysDictTypeDTO.getDictType()).build());
        if (StringUtils.isNotNull(info) && !info.getDictTypeId().equals(dictId)) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }
}
